Skip to content

Conversation

@ZERICO2005
Copy link
Contributor

@ZERICO2005 ZERICO2005 commented Mar 20, 2025

Functions added:

  • fprintf/vfprintf
  • asprintf/vasprintf
  • ti_sprintf,ti_snprintf, and ti_asprintf to <ce/include/ti_sprintf.h>

fprintf redirects the output of nano_printf to fputc

asprintf is implemented via two calls to snprintf #543

ti_sprintf makes explicit calls to the C89 sprintf from OS_LIBC. This can reduce binary sizes significantly compared to nano_printf

ti_snprintf and ti_asprintf write to unmapped memory 0xFC1000 to get the size of the output. Writing to 0xFB0000 also works, but will output to the CEmu console. Otherwise, writing to 0xFC1000 should be okay unless the output exceeds ~258000 characters. Because ti_vsprintf doesn't exist, they are both implemented as macros, and will evaluate __VA_ARGS__ twice.

ti_asprintf will malloc a buffer that ti_sprintf will write to, returning NULL on malloc failure. The buffer shall be deallocated with free

ti_snprintf will return an empty string if the result of ti_sprintf won't fit inside the buffer. This was done to make the routine more compact, as implementing the truncation behavior of snprintf would require calls to malloc. If the user wants the output to be truncated, they can use ti_asprintf and strncpy

char buf[20];
char* temp;
ti_asprintf(&temp, format, ...);
if (temp != NULL) {
	strncpy(buf, temp, sizeof(buf));
	free(temp);
}

@adriweb adriweb linked an issue Mar 20, 2025 that may be closed by this pull request
@adriweb
Copy link
Member

adriweb commented Mar 20, 2025

Nice job, looks good 👍
Can you add a few autotests? 👀

@ZERICO2005
Copy link
Contributor Author

Tests for fprintf, asprintf, ti_sprintf, ti_snprintf, and ti_asprintf have been added now.
Also added stpcpy and memccpy from @calc84maniac along with tests for them.

@ZERICO2005
Copy link
Contributor Author

I have been given the go ahead from @adriweb to merge this PR

@mateoconlechuga
Copy link
Member

I am a bit worried that the ti_ prefix may conflict with the naming convention fileioc.

@adriweb
Copy link
Member

adriweb commented Mar 22, 2025

Gotta trust the tests 🫡

@ZERICO2005
Copy link
Contributor Author

ZERICO2005 commented Mar 22, 2025

I am a bit worried that the ti_ prefix may conflict with the naming convention fileioc.

The functions I've added use this naming convention ti_function, while the ones in fileioc use ti_Function. I guess they could also be ce_sprintf or etc. Also, ti_PutC seems to be a proprietary function, while ti_sprintf is just C89 sprintf, and can be used instead of the sprintf implementation from nano_printf.c in some cases

@ZERICO2005
Copy link
Contributor Author

the function naming should be okay as is, and won't conflict

@ZERICO2005 ZERICO2005 merged commit 3a684ba into master Mar 22, 2025
9 checks passed
@ZERICO2005 ZERICO2005 deleted the pr/asprintf_fprintf branch March 22, 2025 21:03
@mateoconlechuga
Copy link
Member

lol wtf

@adriweb
Copy link
Member

adriweb commented Mar 22, 2025

I'm not sure this is an issue now but if we want to change the naming convention to ce_xxx instead, we can always do that before a release anyway, no worries. Do you think it's better than ti_xxx ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

Add asprintf vasprintf

3 participants